package com.ibeeking.found.upms.cs.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.entity.LoginUserDTO;
import com.ibeeking.found.common.enums.member.MemberSexEnum;
import com.ibeeking.found.upms.api.common.query.AccountLoginQuery;
import com.ibeeking.found.upms.cs.rest.service.ICsPermissionService;
import com.ibeeking.found.upms.cs.rest.service.ICsUserService;
import com.ibeeking.found.upms.service.common.param.CsUserParam;
import com.ibeeking.found.upms.service.common.param.ModifyMobileParam;
import com.ibeeking.found.upms.service.common.param.UserFindPwdParam;
import com.ibeeking.found.upms.service.common.param.UserModifyPwdParam;
import com.ibeeking.found.upms.service.common.query.CsUserPageQuery;
import com.ibeeking.found.upms.service.common.vo.CsUserPageVO;
import com.ibeeking.found.upms.service.common.vo.CsUserVO;
import com.ibeeking.found.upms.service.common.vo.CurrentCsUserVO;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @ClassName UserController控制器
 * @Description 全局用户管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/user/v1")
public class CsUserController {

    @Resource
    private ICsUserService userServiceImpl;
    @Resource
    private ICsPermissionService permissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户权限菜单")
    @GetMapping(value = "/currentPermission")
    public List<CurrentPermissionVO> currentPermission(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        List<CurrentPermissionVO> result = permissionServiceImpl.queryEffectiveList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
        return TreeFastUtils.listToTree(result, GlobalConstant.PARENT_ID);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询客服用户列表")
    @PostMapping(value = "/pageList")
    public Page<CsUserPageVO> pageList(@RequestBody CsUserPageQuery query) {
        return userServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户信息")
    @GetMapping(value = "/currentInfo")
    public CurrentCsUserVO currentPermission(@LoginUser(isFull = true) CurrentUser currentUser) {
        CurrentCsUserVO currentUserVO = BeanUtil.convertBean(currentUser, CurrentCsUserVO.class);
        currentUserVO.setSexName(MemberSexEnum.getDescByCode(currentUserVO.getSex()));
        //查询所属租户logo
        currentUserVO.setTenantLogo(userServiceImpl.queryTenantLogo());
        return currentUserVO;
    }


    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改个人信息")
    @PostMapping(value = "/modifySelf")
    public Boolean modifySelf(@LoginUser CurrentUser currentUser, @RequestBody CsUserParam param) {
        param.setId(currentUser.getId());
        return userServiceImpl.modifySelf(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询客服用户信息")
    @GetMapping(value = "/view/{id}")
    public CsUserVO view(@PathVariable("id") Long id) {
        return userServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改客服用户密码")
    @PostMapping(value = "/modifyPwd")
    public Boolean modifyPwd(@LoginUser CurrentUser currentUser, @RequestBody @Validated UserModifyPwdParam userModifyPwdParam) {
        return userServiceImpl.modifyPwd(currentUser.getId(), userModifyPwdParam);
    }

    /**
     * 找回密码
     *
     * @param userFindPwdParam
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "找回客服用户密码")
    @PostMapping(value = "/findPwd")
    public Boolean findPwd(@RequestBody @Validated UserFindPwdParam userFindPwdParam) {
        return userServiceImpl.findPwd(userFindPwdParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询是否存在账号")
    @IgnoreDS
    @GetMapping(value = "/queryAccount/{mobile}")
    public Boolean del(@PathVariable("mobile") @NotBlank(message = "手机号必填") String mobile) {
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(mobile);
        LoginUserDTO loginUserDTO = userServiceImpl.queryAccountLogin(query);
        return null != loginUserDTO;
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改手机号")
    @PostMapping(value = "/modifyMobile")
    public Boolean modifyMobile(@LoginUser CurrentUser currentUser, @RequestBody @Validated ModifyMobileParam param) {
        return userServiceImpl.modifyMobile(currentUser.getId(), param);
    }
}
